const App = getApp()

Page({
    data: {
      addressId: undefined,
      
      addUrl: App.globalData.baseUrl +"/useraddressinfo/insertuseraddressinfo",
      modifyUrl: App.globalData.baseUrl +"/useraddressinfo/updateuseraddress",


      /////
      
    },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      addressId: options.addressId
    });

    if (options.addressId == undefined) {
      let buyerOpenid = wx.getStorageSync("openid");
      console.log('openid:' + buyerOpenid);
      that.setData({
        buyerOpenid: buyerOpenid
        
      });
      return;
    }
    wx.request({
      url: App.globalData.baseUrl +"/useraddressinfo/getaddressinfobyid",
      data: { "addressId": options.addressId },
      method: 'GET',
      success: function (res) {
        var userAddressinfo = res.data.userAddressinfo;
        if (userAddressinfo == undefined) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            addressUserName: userAddressinfo.addressUserName,
            addressBuyerPhone: userAddressinfo.addressBuyerPhone,
            addressArea: userAddressinfo.addressArea,
            addressDetail: userAddressinfo.addressDetail,
            buyerOpenid: userAddressinfo.buyerOpenid
          });
        }
      }
    })
    },

  formSubmit: function (e) {
    var that = this;
    
    
    var formData = e.detail.value;
    console.log('formData:' + formData);
    var url = that.data.addUrl;
    if (that.data.addressId != undefined) {
      formData.addressId = that.data.addressId;
      url = that.data.modifyUrl;
    }
    wx.request({
      
      url: url,
      
      data: JSON.stringify(e.detail.value),
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var result = res.data.success
        var toastText = "操作成功！";
        if (result != true) {
          toastText = "操作失败" + res.data.errMsg;
        }
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
        if (that.data.addressId == undefined) {
          wx.redirectTo({
            url: '../address/list/index',
          })
        }
      }
    })
  },
    onShow() {
    	/*this.renderForm(this.data.id)*/
    },
    renderForm(id) {
    	// App.HttpService.getAddressDetail(id)
    	/*this.address.getAsync({id: id})
		.then(data => {
			console.log(data)
			if (data.meta.code == 0) {
				const params = {
					name   : data.data.name, 
					gender : data.data.gender, 
					tel    : data.data.tel, 
					address: data.data.address, 
					is_def : data.data.is_def, 
				}

				const radio = this.data.radio
				radio.forEach(n => n.checked = n.value === data.data.gender)

				this.setData({
					show : !params.is_def, 
					radio: radio, 
					form : params, 
				})
			}
		})*/
    },
	submitForm(e) {
		/*const params = e.detail.value

		console.log(params)

		if (!this.WxValidate.checkForm(e)) {
			const error = this.WxValidate.errorList[0]
			App.WxService.showModal({
				title: '友情提示', 
					content: `${error.param} : ${error.msg}`, 
					showCancel: !1, 
			})
			return false
		}

		// App.HttpService.putAddress(id, params)
		this.address.updateAsync({id: id}, params)
		.then(data => {
			console.log(data)
			if (data.meta.code == 0) {
				this.showToast(data.meta.message)
			}
		})*/
	},
	delete() {
		// App.HttpService.deleteAddress(this.data.id)
		/*this.address.deleteAsync({id: this.data.id})
		.then(data => {
			console.log(data)
			if (data.meta.code == 0) {
				this.showToast(data.meta.message)
			}
		})
	},
	showToast(message) {
		App.WxService.showToast({
			title   : message, 
			icon    : 'success', 
			duration: 1500, 
		})
		.then(() => App.WxService.navigateBack())
	},
	chooseLocation() {
		App.WxService.chooseLocation()
	    .then(data => {
	        console.log(data)
	        this.setData({
	        	'form.address': data.address
	        })
	    })*/
	},
})